package com.larsgard.gedviewer.server.services;

import java.util.ArrayList;

import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.larsgard.gedviewer.client.service.PersonSearchService;
import com.larsgard.gedviewer.server.storage.PersonStorage;
import com.larsgard.gedviewer.server.storage.entities.JDOPerson;
import com.larsgard.gedviewer.shared.ajax.SearchRequest;
import com.larsgard.gedviewer.shared.ajax.SearchResult;
import com.larsgard.gedviewer.shared.entities.Person;

@RemoteServiceRelativePath("personsearch")
public class PersonSearchServiceImpl extends RemoteServiceServlet implements PersonSearchService {

    private static final long serialVersionUID = 4348137724994493085L;

    @Override
    public SearchResult search(SearchRequest request) {

	SearchResult result = new SearchResult();
	result.setType(request.getType());
	if (request.getPassword() == null) {
	    // do nothing
	} else {
	    switch (request.getType()) {
	    case LIST_OF_PERSONS:
		ArrayList<JDOPerson> jdopersons = PersonStorage.getInstance().getPersons(request);
		ArrayList<Person> persons = new ArrayList<Person>();
		for (JDOPerson person : jdopersons) {
		    persons.add(person.getPerson());
		}
		result.setPersons(persons);
		break;
	    case DETAIL_FOR_PERSON:
		result.setPersonDetails(PersonStorage.getInstance().getPersonDetails(
			request.getIdno(), request.getDepth()));
	    default:
		break;
	    }
	}
	return result;
    }

}
